package com.meixiang.fragment.shopping;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.shopping.GoodsDetailActivity;
import com.meixiang.adapter.shopping.GoodsImageTextAdapter;
import com.meixiang.entity.shopping.ImageTextEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.view.dragview.DragListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 商品图文信息
 *
 * @author LZRUI
 */
public class GoodsImageTextFragment extends BaseFragment {

    private static String GOODS_ID = "goods_id";
    private FragmentActivity mActivity;

    @Bind(R.id.ry_image)
    DragListView ryImage;
//    @Bind(R.id.ry_image)
//    ListView ryImage;
    @Bind(R.id.tv_hint)
    TextView tvHint;


    private String goodsId;
    private GoodsImageTextAdapter adapter;

    public GoodsImageTextFragment() {
    }

    public static GoodsImageTextFragment newInstance(String goodsId) {
        GoodsImageTextFragment fragment = new GoodsImageTextFragment();
        Bundle bundle = new Bundle();
        bundle.putString(GOODS_ID, goodsId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            goodsId = getArguments().getString(GOODS_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_goods_image_text, container, false);
        ButterKnife.bind(this, rootView);
        mActivity=getActivity();
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
//        ryImage.setOverScrollMode(android.view.View.OVER_SCROLL_NEVER);
        ryImage.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
        ryImage.setAdapter(adapter = new GoodsImageTextAdapter(mActivity));
    }

    @Override
    protected void setData() {
        HttpParams params = new HttpParams();
        params.put("goodsId", goodsId);

        Log.i("lzrtest", "图文信息：params " + params);

        HttpUtils.post(Config.GOODS_IMAGE_TEXT, GoodsDetailActivity.TAG, params, new HttpCallBack(activity) {

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                try {
                    Log.e("TAG","输出图片信息=="+result.toString());
                    List<ImageTextEntity> imageMultiple = new Gson().fromJson(result.getString("imageMultiple"), new TypeToken<List<ImageTextEntity>>() {}.getType());
                    if (imageMultiple != null && !imageMultiple.isEmpty()) {
                        adapter.refreshData(imageMultiple);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String code, String message) {

            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                if (adapter.getCount() == 0) {
                    if (ryImage != null && tvHint != null) {
                        ryImage.setVisibility(View.GONE);
                        tvHint.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (ryImage != null && tvHint != null) {
                        ryImage.setVisibility(View.VISIBLE);
                        tvHint.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
